﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text;
using PowerCreator.LMS.Web.Question;
using PowerCreator.LMS.Question.Entiry;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;

public partial class sysadmin_QuestionManager_CombinationQuestion_Edit_duox : PowerCreator.LMS.Web.Base.BasePage
{
    ManagerQuestion objQuestion = new ManagerQuestion();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string QuestionID = Base64.Decode(Request.QueryString["qid"].ToString());
            string FatherQuestionID = Base64.Decode(Request.QueryString["fthID"].ToString());
            ViewState["QuestionID"] = QuestionID;
            ViewState["FatherQuestionID"] = FatherQuestionID;
            ViewState["TypeID"] = 3;


            Pageload();
            InitPage();
        }
    }
    protected override void InitPage()
    {
        lab_TitleInfo.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_ContentTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_QuestionCount.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionContent", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_QuestionOption.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionOption", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_RightAnswer.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_RightAnswer", PowerCreator.LMS.Language.LanagetItemType.Page);

        btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);


        btn_AddOptionC.Text = GetLanageValueByKey("btn_AddQuestionInfo_danx_AddOption", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_AddOptionD.Text = GetLanageValueByKey("btn_AddQuestionInfo_danx_AddOption", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_AddOptionE.Text = GetLanageValueByKey("btn_AddQuestionInfo_danx_AddOption", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }

    private void bandList(int ItemCount)
    {
        ListItem objlist;
        for (int i = 0; i < ItemCount; i++)
        {
            objlist = new ListItem();
            switch (i)
            {
                case 0: objlist.Text = "A";

                    break;
                case 1: objlist.Text = "B";
                    break;
                case 2: objlist.Text = "C";
                    break;
                case 3: objlist.Text = "D";
                    break;
                case 4: objlist.Text = "E";
                    break;
                case 5: objlist.Text = "F";
                    break;
            }
            objlist.Value = i.ToString();

            chbox_answerlist.Items.Add(objlist);
        }
    }

    private void Pageload()
    {

        VOQuestion objQuestionInfo = new VOQuestion();
        objQuestionInfo = objQuestion.LoadQuestion(int.Parse(ViewState["QuestionID"].ToString()));
        ViewState["objQuestionInfo"] = objQuestionInfo;

        DataTable dt_QuestionOption = new DataTable();
        string search = "FKQuestionOptionQuestionID=" + ViewState["QuestionID"].ToString();
        dt_QuestionOption = objQuestion.QueryQuestionOption(search);
        ViewState["dt_QuestionOption"] = dt_QuestionOption;

        bandList(dt_QuestionOption.Rows.Count);


        txtQuestionContent.Value = objQuestionInfo.QuestionContent;


        #region 绑定选项
        string[] rightAnswerList = objQuestionInfo.QuestionAnswer.Split('$');
        for (int i = 0; i < rightAnswerList.Length; i++)
        {
            if (rightAnswerList[i].ToString() == "A")
            {
                chbox_answerlist.Items[0].Selected = true;
            }
            else if (rightAnswerList[i].ToString() == "B")
            {
                chbox_answerlist.Items[1].Selected = true;
            }
            else if (rightAnswerList[i].ToString() == "C")
            {
                chbox_answerlist.Items[2].Selected = true;
            }
            else if (rightAnswerList[i].ToString() == "D")
            {
                chbox_answerlist.Items[3].Selected = true;
            }
            else if (rightAnswerList[i].ToString() == "E")
            {
                chbox_answerlist.Items[4].Selected = true;
            }
            else
            {
                chbox_answerlist.Items[5].Selected = true;
            }
        }

        #endregion

        #region 绑定选项内容



        for (int i = 0; i < dt_QuestionOption.Rows.Count; i++)
        {
            string Content = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString();
            string Option = Content.Substring(0, 1);
            if (Option == "A")
            {
                txtOptionA.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }
            else if (Option == "B")
            {
                txtOptionB.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }
            else if (Option == "C")
            {
                txtOptionC.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }
            else if (Option == "D")
            {
                txtOptionD.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }
            else if (Option == "E")
            {
                txtOptionE.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }
            else
            {
                txtOptionF.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }

        }
        #endregion

        #region 隐藏未选的选项
        //if (dt_QuestionOption.Rows.Count == 1)
        //{
        //    btn_AddOptionA.Visible = true;
        //    Pnl_B.Visible = false;
        //    Pnl_C.Visible = false;
        //    Pnl_D.Visible = false;

        //    Pnl_E.Visible = false;
        //    Pnl_F.Visible = false;
        //}
        //if (dt_QuestionOption.Rows.Count == 2)
        //{
        //    btn_AddOptionA.Visible = false;
        //    Pnl_B.Visible = true;

        //    Pnl_C.Visible = false;
        //    Pnl_D.Visible = false;

        //    Pnl_E.Visible = false;
        //    Pnl_F.Visible = false;
        //}
        if (dt_QuestionOption.Rows.Count == 3)
        {
            //btn_AddOptionA.Visible = false;

            //Pnl_B.Visible = true;
            //btn_AddOptionB.Visible = false;

            btn_AddOptionC.Visible = true;
            Pnl_D.Visible = false;

            Pnl_E.Visible = false;
            Pnl_F.Visible = false;
        }
        if (dt_QuestionOption.Rows.Count == 4)
        {
            //btn_AddOptionA.Visible = false;

            //Pnl_B.Visible = true;
            //btn_AddOptionB.Visible = false;

            //Pnl_C.Visible = true;
            btn_AddOptionC.Visible = false;

            Pnl_D.Visible = true;

            Pnl_E.Visible = false;
            Pnl_F.Visible = false;
        }
        if (dt_QuestionOption.Rows.Count == 5)
        {
            //btn_AddOptionA.Visible = false;

            //Pnl_B.Visible = true;
            //btn_AddOptionB.Visible = false;

            //Pnl_C.Visible = true;
            btn_AddOptionC.Visible = false;

            Pnl_D.Visible = true;
            btn_AddOptionD.Visible = false;

            Pnl_E.Visible = true;
            Pnl_F.Visible = false;
        }
        if (dt_QuestionOption.Rows.Count == 6)
        {
            //btn_AddOptionA.Visible = false;

            //Pnl_B.Visible = true;
            //btn_AddOptionB.Visible = false;

            //Pnl_C.Visible = true;
            btn_AddOptionC.Visible = false;

            Pnl_D.Visible = true;
            btn_AddOptionD.Visible = false;

            Pnl_E.Visible = true;
            btn_AddOptionE.Visible = false;
            Pnl_F.Visible = true;
        }
        #endregion
    }

    private bool PageCheck()
    {

        if (txtOptionA.Text.Trim() == "" | txtOptionB.Text.Trim() == "" | txtOptionC.Text.Trim() == "")
        {
            Script.Alert(this, "msg",  GetLanageValueByKey("msg_AddQuestionInfo_checkmsg3", PowerCreator.LMS.Language.LanagetItemType.Page));
            return false;
        }
        return true;
    }
    private string ConvertOptionID(int Index)
    {
        string OptionID = string.Empty;
        switch (Index)
        {
            case 0: OptionID = "A";
                break;
            case 1: OptionID = "B";
                break;
            case 2: OptionID = "C";
                break;
            case 3: OptionID = "D";
                break;
            case 4: OptionID = "E";
                break;
            case 5: OptionID = "F";
                break;
        }
        return OptionID;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!PageCheck())
        {
            return;
        }
        StringBuilder str = new StringBuilder();
        ArrayList list = new ArrayList();

        ArrayList Optionlist = new ArrayList();
        Optionlist.Add(txtOptionA.Text.Trim());
        Optionlist.Add(txtOptionB.Text.Trim());
        Optionlist.Add(txtOptionC.Text.Trim());
        Optionlist.Add(txtOptionD.Text.Trim());
        Optionlist.Add(txtOptionE.Text.Trim());
        Optionlist.Add(txtOptionF.Text.Trim());

        int maxindex = 0;
        for (int i = 0; i < chbox_answerlist.Items.Count; i++)
        {
            string OptionID = ConvertOptionID(i);

            if (chbox_answerlist.Items[i].Selected)
            {
                if (str.Length > 0)
                {
                    str.Append("$" + OptionID);
                }
                else
                {
                    str.Append(OptionID);
                }
                if (i > maxindex)
                {
                    maxindex = i;
                }
            }

        }
        for (int j = 0; j < maxindex + 1; j++)
        {
            string OptionID_j = ConvertOptionID(j);

            if (Optionlist[j].ToString() == "")
            {
                Script.Alert(Page, "msg", OptionID_j + GetLanageValueByKey("msg_AddQuestionInfo_checkmsg2", PowerCreator.LMS.Language.LanagetItemType.Page));
                return;
            }
        }
        for (int i = 0; i < Optionlist.Count; i++)
        {
            string OptionID = ConvertOptionID(i);
            if (Optionlist[i].ToString() != "")
            {
                list.Add(OptionID + "、" + Optionlist[i].ToString());
            }
        }

        #region 删除选项

        DataTable dt_QuestionOption = (DataTable)ViewState["dt_QuestionOption"];
        for (int i = 0; i < dt_QuestionOption.Rows.Count; i++)
        {
            string Content = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString();
            string Option = Content.Substring(0, 1);
            string optionID = dt_QuestionOption.Rows[i]["QuestionOptionID"].ToString();
            objQuestion.DeleteQuestionOption(int.Parse(optionID));

        }
        #endregion

        VOQuestion objQuestionInfo = new VOQuestion();
        objQuestionInfo = (VOQuestion)ViewState["objQuestionInfo"];
        //objQuestionInfo.FKQuestionCourseID = int.Parse(ViewState["FKQuestionCourseID"].ToString());
        //objQuestionInfo.QuestionCreateID = int.Parse(Session["UserID"].ToString());
        //info.DateCreated = DateTime.Now;



        objQuestionInfo.QuestionAnswer = str.ToString();
        //objQuestionInfo.QuestionDefaultScore = int.Parse(ViewState["QuestionDefaultScore"].ToString());
        //objQuestionInfo.QuestionDiffculty = int.Parse(ViewState["QuestionDiffculty"].ToString());
        //objQuestionInfo.QuestionKen = ViewState["QuestionKen"].ToString();


        objQuestionInfo.QuestionContent = txtQuestionContent.Value;


        //objQuestionInfo.QuestionState = int.Parse(ViewState["QuestionState"].ToString());     //禁用1,启用2

        VOQuestionOption objQuestionOptionInfo = new VOQuestionOption();

        for (int i = 0; i < list.Count; i++)
        {
            objQuestionOptionInfo.FKQuestionOptionQuestionID = objQuestionInfo.QuestionID;
            objQuestionOptionInfo.QuestionOptionContent = list[i].ToString();
            objQuestionOptionInfo.QuestionOptionCreateID = objQuestionInfo.QuestionCreateID;
            objQuestionOptionInfo.QuestionOptionCreateTime = DateTime.Now;
            objQuestion.CreateQuestionOption(objQuestionOptionInfo);
        }



        if (objQuestion.UpdateQuetion(objQuestionInfo))//更新试题信息
        {
            Script.ShowAndHistory(GetLanageValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page), -2);
        }
        else
        {
            Script.Alert(GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
        }

    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Script.RedirectURL("EditQuestionInfo_zuhe.aspx?qid=" + ViewState["FatherQuestionID"].ToString());
    }
    protected void btn_AddOptionC_Click(object sender, EventArgs e)
    {
        ListItem objlist = new ListItem();
        objlist.Text = "D";
        objlist.Value = "3";
        chbox_answerlist.Items.Add(objlist);
        Pnl_D.Visible = true;
        btn_AddOptionC.Visible = false;
    }
    protected void btn_AddOptionD_Click(object sender, EventArgs e)
    {
        ListItem objlist = new ListItem();
        objlist.Text = "E";
        objlist.Value = "4";
        chbox_answerlist.Items.Add(objlist);
        Pnl_E.Visible = true;
        btn_AddOptionD.Visible = false;
    }
    protected void btn_AddOptionE_Click(object sender, EventArgs e)
    {
        ListItem objlist = new ListItem();
        objlist.Text = "F";
        objlist.Value = "5";
        chbox_answerlist.Items.Add(objlist);
        Pnl_F.Visible = true;
        btn_AddOptionE.Visible = false;
    }
}
